package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class StudentServiceImpl extends BaseService implements IStudentService {

    @Override
    public void add(StudentBean student) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.addStudent(student);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delStudent(id);
        session.commit();
        session.close();
    }

    @Override
    public List<StudentBean> findByStudent(String name, String classroom, String address) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean> list = mapper.findByItem(name,classroom,address);
        session.close();
        return list;
    }

    @Override
    public StudentBean findById(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean student = mapper.findById(id);
        session.close();
        return student;
    }

    @Override
    public void update(int id, int roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.updateRoom(id,roomId);
        session.commit();
        session.close();
    }

    @Override
    public CutPageBean<StudentBean> cutByItem(int pageNO, String name, String classroom, String address) {
        CutPageBean<StudentBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        cutBean.setList(mapper.cutItemList((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE
                ,name,classroom ,address ));
        cutBean.setCount(mapper.cutItemCount(name,classroom,address));
        session.close();
        return cutBean;
    }


    public static void main(String[] args) {
        IStudentService service = new StudentServiceImpl();
//        System.out.println(service.findByStudent(null,"2",null));
//        System.out.println(service.findById(5));
        System.out.println(service.cutByItem(1,null,null,null));
    }
}
